package cn.cdeden.resource.unified.controller;

import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.resource.api.domain.RemoteFile;
import cn.cdeden.resource.domain.bo.SysOssRecentlyViewedBo;
import cn.cdeden.resource.domain.vo.SysOssDownloadRecordVo;
import cn.cdeden.resource.domain.vo.SysOssRecentlyViewedVo;
import cn.cdeden.resource.domain.vo.SysOssVersionVo;
import cn.cdeden.resource.service.ISysOssRecentlyViewedService;
import cn.cdeden.resource.unified.domain.dto.MyOnlinePreviewInfoDto;
import cn.cdeden.resource.unified.domain.dto.OnlinePreviewInfoDto;
import cn.cdeden.resource.unified.service.RemoteUploadFileFilter;
import cn.cdeden.resource.unified.service.UnifiedFileService;
import cn.cdeden.system.api.RemoteUserService;
import cn.cdeden.system.api.model.LoginUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件统一的处理记录模块
 *
 * @ProjectName : KnowCore
 * @Package : cn.cdeden.resource.unified.controller
 * @ClassName : UnifiedFileRecordController
 * @Author : Yu.
 * @Description : //
 * @Date : 2024/12/22 20:34
 * @Version : 1.0
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oss/file/date")
public class UnifiedFileRecordController {


    private final UnifiedFileService uploadFileService;

    private final RemoteUploadFileFilter unificationFile;

    @DubboReference
    private final RemoteUserService userService;


    private final ISysOssRecentlyViewedService viewedService; //浏览记录


    /**
     * 文件的下载记录
     *
     * @param info
     * @return void
     * @author Yu.
     * @Date 2024/12/9 10:28
     **/
    @PostMapping(value = "/downloadRecord")
    public TableDataInfo downloadRecord(@Validated @RequestBody OnlinePreviewInfoDto info, PageQuery pageable) {
        String type = info.getType();
        ////有问题的数据直接抛出异常即可
        RemoteFile dataInfo = unificationFile.getFileDataInfo(info.getModuleId(), LoginHelper.getUserId(), info.getInfo(), OssModuleOperate.Version_DownloadRecord,type); //获取文件的的信息
        TableDataInfo<SysOssDownloadRecordVo> filesDownloadRecord = uploadFileService.getFilesDownloadRecord(dataInfo, info, pageable);
        return filesDownloadRecord;
    }




    /**
     * 查询 文件历史版本查看
     *
     * @param info
     * @param pageable
     * @return cn.cdeden.common.core.utils.page.TableDataInfo<cn.cdeden.resource.domain.vo.SysOssVersionVo>
     * @author Yu.
     * @Description //
     * @Date 2024/12/8 17:03
     **/
    @PostMapping(value = "/getFilesVersions")
    public TableDataInfo<SysOssVersionVo> getFilesVersions(@Validated @RequestBody OnlinePreviewInfoDto info, PageQuery pageable) {
        String type = info.getType();

        RemoteFile dataInfo = unificationFile.getFileDataInfo(info.getModuleId(), LoginHelper.getUserId(), info.getInfo(), OssModuleOperate.Version_DownloadRecord,type); //获取文件的的信息
        TableDataInfo<SysOssVersionVo> filesVersions = uploadFileService.getFilesVersions(dataInfo, info, pageable);
        return filesVersions;
    }



    /**
     * 查询 文件预览记录
     *
     * @param info
     * @param pageable
     * @return cn.cdeden.common.core.utils.page.TableDataInfo<cn.cdeden.resource.domain.vo.SysOssVersionVo>
     * @author Yu.
     * @Date 2024/12/8 17:03
     **/
    @PostMapping(value = "/getPreviewRecord")
    public TableDataInfo<SysOssRecentlyViewedVo> getPreviewRecord(@Validated @RequestBody OnlinePreviewInfoDto info, PageQuery pageable) {
        String type = info.getType();
        RemoteFile dataInfo = unificationFile.getFileDataInfo(info.getModuleId(), LoginHelper.getUserId(), info.getInfo(), OssModuleOperate.Version_DownloadRecord, type);
        SysOssRecentlyViewedBo build = SysOssRecentlyViewedBo.builder()
            .ossId(dataInfo.getOssId())
            .moduleId(info.getModuleId())
            .moduleType(info.getType())
            .build();
        TableDataInfo<SysOssRecentlyViewedVo> previewRecord =  viewedService.selectRecordPage(build,false , pageable);
        return previewRecord;
    }

    /**
     * 查询自己的 文件预览记录
     * @author Yu.
     * @Date 2024/12/22 21:13
     * @param info
     * @param pageable
     * @return cn.cdeden.common.core.utils.page.TableDataInfo<cn.cdeden.resource.domain.vo.SysOssRecentlyViewedVo>
     **/
    @PostMapping(value = "/module/my/record")
    public TableDataInfo<SysOssRecentlyViewedVo> getModuleTypeRecord(@Validated @RequestBody MyOnlinePreviewInfoDto info, PageQuery pageable) {
        if (info.getType() != null){
            OssModuleType coedInfo = OssModuleType.findCoedInfo(info.getType());
            if (coedInfo == null){
                throw new IllegalStateException("Unexpected value: " + info.getType());
            }

        }

        LoginUser loginUser = LoginHelper.getLoginUser();  //获取用户信息
        SysOssRecentlyViewedBo build = SysOssRecentlyViewedBo.builder()
            .moduleId(info.getModuleId())
            .moduleType(info.getType())
            .createBy(loginUser.getUserId())
            .build();
        TableDataInfo<SysOssRecentlyViewedVo> previewRecord = viewedService.selectRecordPage(build, true, pageable);


        return previewRecord;
    }














}
